﻿using System;
using System.Collections.Generic;
using System.Text;
using SharpDiary.Core.Data;
using SharpDiary.Core.Common;
using SharpDiary.Core.Security;
using SharpDiary.Core.Utility;

namespace SharpDiary.Core.ORMapping
{
    /// <summary>
    /// 日记。
    /// </summary>
    [TableInfoAttribute("Diary")]
    public class DiaryInfo : DbMappingObjectBase
    {
        #region Fields & Properties

        private string _id = string.Empty;

        [FieldMapping(true, System.Data.DbType.String, false, true)]
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private Int64 _diaryGroupId = 1;

        /// <summary>
        /// 获取或设置所属日记组代码。
        /// </summary>
        [FieldMapping(false, System.Data.DbType.Int64, false, false)]
        public Int64 DiaryGroupId
        {
            get { return _diaryGroupId; }
            set { _diaryGroupId = value; }
        }

        private string _title = string.Empty;

        /// <summary>
        /// 获取或设置日记标题。
        /// </summary>
        [FieldMapping(false, System.Data.DbType.String, false, false)]
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        private string _content = string.Empty;

        /// <summary>
        /// 获取或设置日记内容。
        /// </summary>
        [FieldMapping(false, System.Data.DbType.String, false, false)]
        public string Content
        {
            get
            {
                return _content;
            }
            set
            {
                _content = value;
            }
        }

        private string _tag = string.Empty;

        /// <summary>
        /// 获取或设置日记标签。(多个标签以英文逗号分隔)
        /// </summary>
        [FieldMapping(false, System.Data.DbType.String, false, false)]
        public string Tag
        {
            get { return _tag; }
            set { _tag = value; }
        }

        private DateTime _createdDatetime = DateTime.Now;

        /// <summary>
        /// 获取或设置日记创建时间。
        /// </summary>
        [FieldMapping(false, System.Data.DbType.DateTime, false, false)]
        public DateTime CreatedDatetime
        {
            get { return _createdDatetime; }
            set { _createdDatetime = value; }
        }

        private DateTime _updateDatetime = DateTime.Now;

        [FieldMapping(false, System.Data.DbType.DateTime, false, false)]
        public DateTime UpdateDatetime
        {
            get { return _updateDatetime; }
            set { _updateDatetime = value; }
        }

        private Int64 _weatherCode = -1;

        [FieldMapping(false, System.Data.DbType.Int64, false, false)]
        public Int64 WeatherCode
        {
            get { return _weatherCode; }
            set { _weatherCode = value; }
        }

        private Int64 _emotionId = -1;

        [FieldMapping(false, System.Data.DbType.Int64, false, false)]
        public Int64 EmotionId
        {
            get { return _emotionId; }
            set { _emotionId = value; }
        }

        private Int64 _searchFlag = 0;

        /// <summary>
        /// 用于搜索时标识关键字的匹配状态。(1: 匹配 0:不匹配)
        /// </summary>
        [FieldMapping(false, System.Data.DbType.Int64, false, false)]
        public Int64 SearchFlag
        {
            get { return _searchFlag; }
            set { _searchFlag = value; }
        }

        private string _privatePasswordMD5 = string.Empty;

        /// <summary>
        /// 独立密码。如果不为空，则只有输入正确的密码后，才可查看日记内容。
        /// </summary>
        [FieldMapping(false, System.Data.DbType.String, false, false)]
        public string PrivatePasswordMD5
        {
            get { return _privatePasswordMD5; }
            set { _privatePasswordMD5 = value; }
        }

        #endregion

        #region Constructors

        public DiaryInfo()
        {
        }

        #endregion

        #region Methods

        /// <summary>
        /// 获取日记总数。
        /// </summary>
        /// <returns>日记总数</returns>
        public static int GetTotalDiaryCount()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select count(*) as FCount from Diary");

            DataAccessService das = Utils.GetDataAccessServiceInstance();
            object result = das.ExecuteScalar(sql.ToString());

            return (result != null && result != DBNull.Value ? Convert.ToInt32(result) : 0);
        }

        /// <summary>
        /// 获取所有日记。
        /// </summary>
        /// <returns></returns>
        public static List<DiaryInfo> GetAllDiaries()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("SELECT * FROM Diary");

            DataAccessService das = Utils.GetDataAccessServiceInstance();
            return DbObjectMappingHelper.QueryObjectList<DiaryInfo>(sql.ToString());
        }

        public override string ToString()
        {
            return (!string.IsNullOrEmpty(_title) ? UserSettings.ShellInstance.CryptionService.Decrypt(_title) : string.Empty);
        }

        /// <summary>
        /// 获取指定日期的日记。
        /// </summary>
        /// <param name="dateTime">日期(只按日期筛选，不关心时间)</param>
        /// <returns></returns>
        public static List<DiaryInfo> GetDiaries(DateTime dateTime)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("SELECT * FROM Diary");
            sql.AppendLine("where Date(CreatedDatetime) = @date");

            DataAccessService das = Utils.GetDataAccessServiceInstance();
            return DbObjectMappingHelper.QueryObjectList<DiaryInfo>(sql.ToString(), das.CreateParameter("@date", System.Data.DbType.String,
                dateTime.ToString("yyyy-MM-dd")));
        }

        /// <summary>
        /// 加密日记信息。
        /// </summary>
        /// <param name="diaryInfo">日记信息</param>
        /// <returns>加密后的日记信息</returns>
        public static DiaryInfo EncryptDiaryInfo(DiaryInfo diaryInfo)
        {
            if (diaryInfo == null) { return null; }

            diaryInfo.Content = UserSettings.ShellInstance.CryptionService.Encrypt(diaryInfo.Content);
            diaryInfo.Title = UserSettings.ShellInstance.CryptionService.Encrypt(diaryInfo.Title);

            return diaryInfo;
        }

        /// <summary>
        /// 解密日记信息。
        /// </summary>
        /// <param name="diaryInfo">日记信息</param>
        /// <returns>解密后的日记信息</returns>
        public static DiaryInfo DecryptDiaryInfo(DiaryInfo diaryInfo)
        {
            if (diaryInfo == null) { return null; }

            diaryInfo.Content = UserSettings.ShellInstance.CryptionService.Decrypt(diaryInfo.Content);
            diaryInfo.Title = UserSettings.ShellInstance.CryptionService.Decrypt(diaryInfo.Title);

            return diaryInfo;
        }

        /// <summary>
        /// 更新搜索状态标识。
        /// </summary>
        /// <param name="diaryId">日记编码</param>
        /// <param name="searchFlag">搜索状态标识</param>
        public static bool UpdateSearchFlag(string diaryId, Int64 searchFlag)
        {
            if (string.IsNullOrEmpty(diaryId)) { return false; }

            StringBuilder sql = new StringBuilder();
            sql.AppendLine("update Diary set SearchFlag = @searchFlag");
            sql.AppendLine("where id = @id");

            DataAccessService das = Utils.GetDataAccessServiceInstance();
            return das.ExecuteNonQuery(sql.ToString(), System.Data.CommandType.Text,
                das.CreateParameter("@searchFlag", System.Data.DbType.Int64, searchFlag),
                das.CreateParameter("@id", System.Data.DbType.String, diaryId)) > 0;
        }

        /// <summary>
        /// 重置所有日记的搜索标识为指定值。
        /// </summary>
        /// <param name="newSearchFlag">搜索状态标识</param>
        /// <returns></returns>
        public static bool ResetSearchFlag(Int64 newSearchFlag)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("update Diary set SearchFlag = @searchFlag");

            DataAccessService das = Utils.GetDataAccessServiceInstance();
            return das.ExecuteNonQuery(sql.ToString(), System.Data.CommandType.Text,
                das.CreateParameter("@searchFlag", System.Data.DbType.Int64, newSearchFlag)) > 0;
        }

        #endregion
    }
}
